package com.dpkit.adapter.orm.definition;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 动态实体定义类
 * 描述JSON到实体类的映射规则
 */
public class EntityDefinition {
    // 基础信息
    private String entityName;
    private String tableName;
    private String packageName;
    private IdGenerationStrategy idStrategy;
    
    // 字段定义 (保持插入顺序)
    private final Map<String, FieldDefinition> fields = new LinkedHashMap<>();
    
    // 关联关系
    private List<AssociationDefinition> associations;
    
    // 保存策略
    private SaveStrategy saveStrategy = SaveStrategy.SAVE_OR_UPDATE;
    
    // 枚举定义
    public enum IdGenerationStrategy {
        AUTO, SEQUENCE, UUID, CUSTOM
    }
    
    public enum SaveStrategy {
        SAVE, UPDATE, SAVE_OR_UPDATE
    }

    /**
     * 获取全限定类名
     */
    public String getFullClassName() {
        return (packageName != null && !packageName.isEmpty()) 
            ? packageName + "." + entityName 
            : entityName;
    }

    /**
     * 添加字段定义
     */
    public void addField(FieldDefinition field) {
        this.fields.put(field.getFieldName(), field);
        if (field.isId()) {
            this.idStrategy = field.getIdGenerationStrategy();
        }
    }


    // Getters and Setters
    public String getEntityName() {
        return entityName;
    }

    public void setEntityName(String entityName) {
        this.entityName = entityName;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public IdGenerationStrategy getIdStrategy() {
        return idStrategy;
    }

    public void setIdStrategy(IdGenerationStrategy idStrategy) {
        this.idStrategy = idStrategy;
    }

    public Map<String, FieldDefinition> getFields() {
        return fields;
    }

    public List<AssociationDefinition> getAssociations() {
        return associations;
    }

    public void setAssociations(List<AssociationDefinition> associations) {
        this.associations = associations;
    }

    public SaveStrategy getSaveStrategy() {
        return saveStrategy;
    }

    public void setSaveStrategy(SaveStrategy saveStrategy) {
        this.saveStrategy = saveStrategy;
    }
}